﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TanmiaGrp.GeneralLedger.Core;
using TanmiaGrp.GeneralLedger.Domain;
using TanmiaGrp.Basis;
using System.Windows.Input;
using TanmiaGrp.GeneralLedger.WinApp.Commands;
using System.Windows;

namespace TanmiaGrp.GeneralLedger.WinApp.Scenarios
{
    // TASK: Add ability to filtering upon the PostStatus.
    // TASK: Posted Journal must not be deleted.
    public class JournalBrowserViewModel : BaseBrowserViewModel<Journal, JournalDomain>
    {
        #region Private Fields
        private string postText;

        #endregion Private Fields

        #region Public Properties

        public ICommand PostCommand { get; private set; }

        /// <summary>
        /// Gets the Human-Readable Text of Post/Unpost UIElement.
        /// </summary>
        public string PostText
        {
            get { return postText; }
            private set
            {
                if (postText != value)
                {
                    postText = value;
                    RaisePropertyChanged(() => PostText);
                }
            }
        }

        #endregion

        public JournalBrowserViewModel()
        {
            BusinessDomain = JournalDomain.Obj;

            PostCommand = new DelegateCommand(OnPostHandler, CanPost);

            EntitySavedOrDeletedEvent = Events.JournalEntitySavedOrDeleted;
            EntityNavigationEvent = Events.JournalEntityNavigation;
            BrowserInitializedEvent = Events.JournalBrowserInitialized;
            PageNavigationEvent = Events.JournalPageNavigation;

            Initialize();

            EditorView = ViewNames.JournalEditor;
            BrowserView = ViewNames.JournalBrowser;
        }

        private bool CanPost()
        {
            if (Entities == null) return false;

            Journal currentJournal = Entities.CurrentItem as Journal;
            if (currentJournal == null) return false;

            return true;
        }

        private void OnPostHandler()
        {
            try
            {
                Journal currentJournal = Entities.CurrentItem as Journal;
                string text = currentJournal.PostState == PostStateEnum.IsPosted ? "فك ترحيل" : "ترحيل";

                MessageBoxResult userResponse = UserMessageSvc.RequestUserConfirmation(string.Format("هل أنت متأكد من {0} اليومية المحددة؟", text), string.Format("تأكيد {0}", text));
                if (userResponse == MessageBoxResult.No)
                    return;
                
                if (currentJournal.PostState == PostStateEnum.IsPosted)
                    BusinessDomain.UnPost(currentJournal);
                else
                    BusinessDomain.Post(currentJournal);

                OnRefresh(NavigationMoveEnum.KeepCurrent);
            }
            catch (BaseAppException appEx)
            {
                UserMessageSvc.ShowErrorMessage(appEx);
            }
        }

        protected override bool CanEdit(Journal arg)
        {
            return base.CanEdit(arg) && arg.PostState != PostStateEnum.IsPosted;
        }

        protected override void OnEdit(Journal arg)
        {
            Journal aggregateRoot = BusinessDomain.GetAggregateRoot(arg);
            aggregateRoot.CaptureState();

            UpdateCurrentViewEvent
                .Send(aggregateRoot)
                .Fire(EditorView);
        }

        protected override void UpdateUI()
        {
            if (CanPost())
            {
                Journal currentJournal = Entities.CurrentItem as Journal;
                if (currentJournal != null)
                    PostText = currentJournal.PostState == PostStateEnum.IsPosted ? "فك الترحيل" : "ترحيل";
            }

            PostCommand.RaiseCanExecuteChanged();
            base.UpdateUI();
        }
    }
}
